/**
 * id,pid(列表)转children(树形)
 * @description pid的数据结构转children数据结构
 * @see https://www.cnblogs.com/hanhaihu/p/10114587.html
 */
function PidToChildren(data: any[]) {
  const treeData = [];
  const hash: any = {};
  for (let i = 0; i < data.length; i += 1)
    hash[data[i].id] = data[i];
  for (let i = 0; i < data.length; i += 1) {
    const value = data[i];
    const hashVP = hash[value.pid];
    if (hashVP) {
      !hashVP.children && (hashVP.children = []);
      hashVP.children.push(value);
    } else treeData.push(value);
  }
  return treeData;
}

export { PidToChildren };
